<?php
@header('Content-Type: text/html; charset=utf-8');
date_default_timezone_set("Asia/Shanghai");

class UserInfo
{
  var $uid;
  var $nick;
  var $email;
  var $phone;
  var $status;
  var $ussi_array;

  function __construct($mysql_object, $mysql, $ussi_select_sql)
  {
    $this->uid = $mysql_object->uid;
    $this->nick = $mysql_object->nick;
    $this->email = $mysql_object->email;
    $this->phone = $mysql_object->phone;
    $this->status = $mysql_object->status;
    $this->ussi_array = array();

    $sql = $ussi_select_sql.$this->uid;
    $result = $mysql->query($sql);
    while ($row = $result->fetch_object())
    {
      $ussi = new UserSSInfo($row);
      $this->ussi_array[] = $ussi;
    }
  }
};

class UserSSInfo
{
  var $source;
  var $board;
  var $last_timestamp;
  var $keywords;
  var $keywords_str;
  var $reply_limit;

  function parse_keywords($string)
  {
    $this->keywords = split('[ ,]', $string);
    $this->keywords_str = $string;
    $n = 0;
    foreach ($this->keywords as $i)
    {
      $i = str_replace(" ", "", $i);
      if (0 == strlen($i))
      {
        unset($this->keywords[$n]);
      }
      $n++;
    }
  }

  function __construct($mysql_object)
  {
    $this->source = $mysql_object->source;
    $this->board = $mysql_object->board;
    $this->last_timestamp = $mysql_object->last_timestamp;
    $this->parse_keywords($mysql_object->keywords);
    $this->reply_limit = $mysql_object->reply_limit;
  }
};

////////////////////////////////////////////////////////////////////////////////////////////////////

class BindParam
{
  private $values = array(), $types = '';

  public function add($type, &$value)
  {
    $this->values[] = &$value; 
    $this->types .= $type; 
  } 

  public function get()
  {
    return array_merge(array($this->types), $this->values);
  }
}

class UserData
{
  var $user_info;
  var $ussd_array;

  function get_timestamp($ussd_timestamp)
  {
    if (0 == $ussd_timestamp)
    {
      $timestamp = 0;
    }
    else
    {
      $today = date("Y-m-d ")."00:00:00";
      $timestamp = strtotime($today);
    }
    return $timestamp;
  }

  function __construct($user_info, $ussd_select_stmt)
  {
    $this->user_info = $user_info;
    foreach ($this->user_info->ussi_array as $ussi)
    {
      $timestamp = $this->get_timestamp($ussi->last_timestamp);
      $source_board = $ussi->source.".".$ussi->board;
      $bind_param = new BindParam();
      for ($i = 0; $i < 32; $i++)
      {
        if (isset($ussi->keywords[$i]))
        {
          $bind_param->add("s", $ussi->keywords[$i]);
        }
        else
        {
          $keyword = "";
          $bind_param->add("s", $keyword);
        }
      }
      $bind_param->add("s", $ussi->board);
      $bind_param->add("i", $timestamp);
      $bind_param->add("i", $ussi->reply_limit);

      var_dump($bind_param->get());
      call_user_func_array(array($ussd_select_stmt, 'bind_param'), $bind_param->get());
      //$ussd_select_stmt->bind_param("ssii", $keyword, $ussi->board, $timestamp, $ussi->reply_limit);
      $ussd_select_stmt->execute();
      $ussd = new UserSSData();
      $ussd_select_stmt->bind_result($ussd->url, $ussd->board, $ussd->title, $ussd->reply, $ussd->timestamp);
      while ($ussd_select_stmt->fetch())
      {
        $ussd->keyword = $keyword;
        if (0 == count($this->ussd_array)
            || !in_array($source_board, $this->ussd_array)
            || 0 == count($this->ussd_array[$source_board])
            || !in_array($ussd->url, $this->ussd_array[$source_board]))
        {
          $ussd_repl = new UserSSData();
          $ussd_repl->copy($ussd);
          $this->ussd_array[$source_board][$ussd->timestamp] = $ussd_repl;

          if ($ussi->last_timestamp < $ussd->timestamp)
          {
            $ussi->last_timestamp = $ussd->timestamp;
          }
        }
        //print_r($ussd);
      }

      if (0 != count($this->ussd_array)
          && in_array($source_board, $this->ussd_array)
          && 0 != count($this->ussd_array[$source_board]))
      {
        ksort($this->ussd_array[$source_board]);
      }
    }
    //print_r($this->user_info);
    //print_r($this->ussd_array);
    //echo "======\n";
  }
};

class UserSSData
{
  var $keyword;
  var $url;
  var $board;
  var $title;
  var $reply;
  var $timestamp;

  function copy($other)
  {
    $this->keyword    = $other->keyword;
    $this->url        = $other->url;
    $this->board      = $other->board;
    $this->title      = $other->title;
    $this->reply      = $other->reply;
    $this->timestamp  = $other->timestamp;
  }
};

////////////////////////////////////////////////////////////////////////////////////////////////////

function render_ss_data($user_data)
{
  $htable = "
    你订阅的关键词：
    <table border=\"0.5\">
      <tr>
        <th>版面</th>
        <th>关键词</th>
      </tr>";
  foreach ($user_data->user_info->ussi_array as $ussi)
  {
    $hrow = "
      <tr>
        <td>$ussi->board</td>
        <td>$ussi->keywords_str</td>
      </tr>";
    $htable = $htable.$hrow;
  }
  $htable = $htable."
      </table>";

  if (0 < count($user_data->ussd_array))
  {
    $htable = $htable."
      你订阅的信息：
      <table border=\"0.5\">
        <tr>
          <th>版面</th>
          <th>标题</th>
          <th>回复</th>
          <th>时间</th>
        </tr>";

    foreach ($user_data->ussd_array as $b)
    {
      foreach ($b as $d)
      {
        $date_str = date("Y-m-d H:i:s", $d->timestamp);
        $hrow = "
          <tr>
            <td>$d->board</td>
            <td>
              <a href=\"http://m.newsmth.net$d->url\">$d->title</a>
            </td>
            <td>$d->reply</td>
            <td>$date_str</td>
          </tr>";
        $htable = $htable.$hrow;
      }
    }

    $htable = $htable."
      </table>";
  }
  else
  {
    $htable = $htable."
      <table border=\"0\">
        <tr>
          <th>没有新的订阅消息</th>
        </tr>
      </table>";
  }

  return $htable;
}

?>
